package com.jason.lightreader.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jason.lightreader.response.Code;
import com.jason.lightreader.response.Result;
import com.jason.lightreader.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//认证失败处理流程
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        Result result = Result.get(Code.FAILED,"认证失败！");
        String resultJSONString = objectMapper.writeValueAsString(result);
        //处理异常
        WebUtil.renderString(response,resultJSONString);
    }
}
